﻿using System.Text;
using System.Web;
using System.Web.UI;

namespace Nasa8x.Web.UI
{

    [ToolboxData("<{0}:Pager runat=\"server\"></{0}:Pager>")]
    public sealed class Pager : System.Web.UI.WebControls.Panel
    {

        #region Properties

        private int _pageIndex=1;

        public enum RenderType
        {
            Table = 0,
            Div = 1
        }



        private RenderType _renderMode = RenderType.Table;

        public RenderType RenderMode
        {
            get
            {
                return _renderMode;
            }
            set
            {
                _renderMode = value;
            }
        }

        public string PageItemCssClass
        {

            get
            {
                object _o = ViewState["PageItemCssClass"];

                if (_o != null)
                    return (string)_o;
                return "page-item";
            }
            set
            {
                ViewState["PageItemCssClass"] = value;
            }
        }


        public string PageXOfYCssClass
        {

            get
            {
                object _o = ViewState["PageXOfYCssClass"];

                if (_o != null)
                    return (string)_o;
                return "page-x-of-y";
            }
            set
            {
                ViewState["PageXOfYCssClass"] = value;
            }
        }




        public string PageItemActivedCssClass
        {

            get
            {
                object _o = ViewState["PageItemActivedCssClass"];

                if (_o != null)
                    return (string)_o;
                return "page-item-actived";
            }
            set
            {
                ViewState["PageItemActivedCssClass"] = value;
            }
        }


        public int PageIndex
        {
            get
            {
                string _p = HttpContext.Current.Request.QueryString["P"];
                if (!string.IsNullOrEmpty(_p))
                    _pageIndex= int.Parse(_p);
                return _pageIndex;

            }
            set { _pageIndex = value; }
           
        }

        public int TotalPage
        {
            get;
            set;
        }

        public string PageFormatString
        {
            get;
            set;

        }

        public string PageXOfY
        {
            get;
            set;

        }


        #endregion


        protected override void Render(HtmlTextWriter output)
        {
            //base.Render(output);


            if(TotalPage>1)
            {
                output.Write(PageRender());
            }
            else
            {
                Visible = true;
            }


            
        }


        #region Page Render

        protected string PageRender()
        {

            const int pageButtonCount = 3;
            int min = PageIndex - pageButtonCount;
            int max = PageIndex + pageButtonCount;

            if (max > TotalPage)
                min -= max - TotalPage;
            else if (min < 1)
                max += 1 - min;

            StringBuilder _sb = new StringBuilder(1000);
            bool needDiv = false;
            if (RenderMode == RenderType.Div)
            {
                // _sb.AppendFormat("<div class=\"{0}\">", CssClass);
                for (int i = 1; i <= TotalPage; i++)
                {
                    if (i <= 2 || i > TotalPage - 2 || (min <= i && i <= max))
                    {

                        string _className = (i == PageIndex) ? PageItemActivedCssClass : PageItemCssClass;

                        _sb.AppendFormat("<div onclick=\"javascript:location='{0}'\" class=\"{1}\"><a href=\"{0}\">{2}</a></div>", string.Format(PageFormatString, i), _className, i);
                        needDiv = true;
                    }
                    else if (needDiv)
                    {
                        _sb.AppendFormat("<div class=\"{0}\">...</div>", PageItemCssClass);
                        needDiv = false;
                    }
                }

                //_sb.Append("</div>");
            }
            else
            {
                _sb.AppendFormat("<div class=\"{0}\">", CssClass);
                _sb.AppendFormat("<table cellpadding=\"0\" cellspacing=\"1\" style=\"width:auto;\"><tr>");

                _sb.AppendFormat("<td class=\"{0}\">", PageXOfYCssClass);

                _sb.Append(!string.IsNullOrEmpty(PageXOfY)
                               ? string.Format(PageXOfY, PageIndex, TotalPage)
                               : string.Format("Page {0} of {1}", PageIndex, TotalPage));

                _sb.Append("</td>");


                //Ren Prev Button
                if (PageIndex > 1)
                    _sb.AppendFormat(
                        "<td onclick=\"javascript:location='{0}'\" class=\"{1}\"><a href=\"{0}\">«</a></td>",
                        string.Format(PageFormatString, PageIndex - 1), PageItemCssClass);


                for (int i = 1; i <= TotalPage; i++)
                {
                    if (i <= 2 || i > TotalPage - 2 || (min <= i && i <= max))
                    {

                        string _className = (i == PageIndex) ? PageItemActivedCssClass : PageItemCssClass;

                        _sb.AppendFormat("<td onclick=\"javascript:location='{0}'\" class=\"{1}\"><a href=\"{0}\">{2}</a></td>", string.Format(PageFormatString, i), _className, i);
                        needDiv = true;
                    }
                    else if (needDiv)
                    {
                        _sb.AppendFormat("<td class=\"{0}\">...</td>", PageItemCssClass);
                        needDiv = false;
                    }
                }

                //Ren Next Button
                if (PageIndex < TotalPage)
                    _sb.AppendFormat(
                        "<td onclick=\"javascript:location='{0}'\" class=\"{1}\"><a href=\"{0}\">&#187;</a></td>",
                        string.Format(PageFormatString, PageIndex  +1), PageItemCssClass);

                _sb.Append("</tr></table>");
                _sb.Append("</div>");

            }

            return _sb.ToString();
        }

        //protected string PageRender(string _link, int _totalPage)
        //{

        //    const int pageButtonCount = 3;
        //    int min = PageIndex - pageButtonCount;
        //    int max = PageIndex + pageButtonCount;

        //    if (max > _totalPage)
        //        min -= max - _totalPage;
        //    else if (min < 1)
        //        max += 1 - min;

        //    StringBuilder _sb = new StringBuilder();
        //    _sb.Append("<table cellpadding=\"0\" cellspacing=\"5\" align=\"right\"><tr>");

        //    // Create page buttons
        //    bool needDiv = false;
        //    for (int i = 1; i <= _totalPage; i++)
        //    {
        //        if (i <= 2 || i > _totalPage - 2 || (min <= i && i <= max))
        //        {

        //            string _className = (i == PageIndex) ? "page_item_actived" : "page_item";

        //            _sb.AppendFormat("<td align=\"center\" onclick=\"javascript:location='{0}'\" class=\"{1}\"><a href=\"{0}\">{2}</a></td>", string.Format(_link, i), _className, i);
        //            needDiv = true;
        //        }
        //        else if (needDiv)
        //        {
        //            _sb.Append("<td align=\"center\">...</td>");
        //            needDiv = false;
        //        }
        //    }

        //    _sb.Append("</tr></table>");

        //    return _sb.ToString();
        //}



        #endregion

    }
}
